package me.jiatao.recyclerviewstudy;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by JiaTao on 2018/10/12.
 * Email: 535318720@qq.com
 * Version: 1.0
 * Description:
 */
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyHolder> {

    private Context context;
    private List<String> stringList;

    public RecyclerAdapter(Context context, List<String> stringList) {
        this.context = context;
        this.stringList = stringList;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_recycler, viewGroup, false);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        TextView textView = myHolder.textView;
        textView.setText(stringList.get(i));

        ViewGroup.LayoutParams  lp = textView.getLayoutParams();
        if (i % 2 == 0) {
            lp.width *=2;
        } else if (i % 5 == 0) {
            lp.height *=2;
        }

        textView.setLayoutParams(lp);
    }

    @Override
    public int getItemCount() {
        return stringList.size();
    }


    public void update(List<String> stringList) {
        this.stringList = stringList;
        notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.item_recycler_text);

        }
    }
}
